package com.example.demo.controller;


import com.example.demo.entity.Notice_recption;
import com.example.demo.entity.User;
import com.example.demo.pojo.PasswordSetRequest;
import com.example.demo.pojo.PlannerResult;
import com.example.demo.repository.Notice_recptionRepository;
import com.example.demo.repository.UserRepository;
import com.example.demo.utils.Md5Util;
import com.example.demo.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserActController {

    @Autowired
    UserRepository userRepository;

    @Autowired
    Notice_recptionRepository noticeRecptionRepository;
    //管理员新增用户
    @PostMapping ("/add")
    public PlannerResult<String> adduser(@RequestBody UserVo userVo){
        User u = new User();
        u.setGender(userVo.getGender());
        u.setAvatar(userVo.getAvatar());
        u.setUsername(userVo.getUsername());
        u.setPassword(Md5Util.getMD5String(userVo.getPassword()));
        u.setEmail(userVo.getEmail());
        u.setPhone(userVo.getPhone());
        u.setIs_admin(userVo.getIs_admin());
        LocalDateTime localDateTime1 = LocalDateTime.now();
        LocalDateTime localDateTime2 = LocalDateTime.now();
        u.setCreate_time(localDateTime1);
        u.setUpdate_time(localDateTime2);
        // 将新用户插入数据库
        User user = userRepository.save(u);
        if(user.getId() != null){
            return PlannerResult.success("注册成功!");
        }
        // 注册失败，返回错误
        return PlannerResult.error("注册失败！");
    }

   //分页查询用户
   @PostMapping("/list")
   public PlannerResult<Page<User>> list_user(@RequestBody Map<String, Integer> params) {
       Integer page = params.get("page");
       Integer size = params.get("size");
       Pageable pageable = PageRequest.of(page - 1, size);
       Page<User> result = userRepository.findAll(pageable);
       return PlannerResult.success(result);
   }
   //查看通知

    @GetMapping("/seenotice")
    public PlannerResult<String> see_notice(@RequestParam("noticeid") Integer noticeid,@RequestParam("userid") Integer userid)
    {
        Notice_recption noticeRecption=noticeRecptionRepository.findByNoticeIdAndUserId(noticeid,userid);
        noticeRecption.setState("已接收");
        if(noticeRecption!=null)
        {
            return PlannerResult.success("已查看");
        }
        else
        {
            return PlannerResult.error("error");
        }
    }

    @PostMapping("/setpassword")
    public PlannerResult<String> set_password(@RequestBody PasswordSetRequest passwordSetRequest)
    {
        Integer userid = passwordSetRequest.getUserid();
        String new_password = passwordSetRequest.getNew_password();
        User user = userRepository.findById(userid).get();
        LocalDateTime localDateTime = LocalDateTime.now();
        user.setPassword(new_password);
        user.setUpdate_time(localDateTime);
        User result = userRepository.save(user);
        if(result.getPassword().equals(new_password))
        {
            return PlannerResult.success("成功修改密码");
        }
        else
        {
            return PlannerResult.error("修改失败");
        }
    }

    @PostMapping("/delete/{userid}")
    public PlannerResult<String> deleteUser(@PathVariable("userid") Integer userid) {
        if (userRepository.existsById(userid)) {
            return PlannerResult.error("删除失败");
        } else {
            userRepository.deleteById(userid);
            return PlannerResult.success("删除成功");
        }
    }
}
